package my.georings;

import java.io.IOException;

import my.georings.service.LocationChangeService;
import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.TabHost;

public class MainActivity extends TabActivity  {
    /** Called when the activity is first created. */
	
	private ApplicationStateListener stateListener;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	
        setContentView(R.layout.main);

        Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;
        Intent intent;
        
        ((GeoPhoneRingsApp)getApplication()).init();

        try {
        	((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().deserialize(this);
        	((GeoPhoneRingsApp)getApplication()).getSettings().deserialize(this);
		} catch (IOException e) {
			
		}
        
		Intent svc = new Intent(this, LocationChangeService.class);
		// if service is not running - turn it on
		
		if(!((GeoPhoneRingsApp)getApplication()).checkServiceRunning()){
			((GeoPhoneRingsApp)getApplication()).setServiceWasStarted(false);
			startService(svc);
		}else{
			((GeoPhoneRingsApp)getApplication()).setServiceWasStarted(true);
			stopService(svc);
			startService(svc);
		}
		doBindService();
		
		// map
        intent = new Intent().setClass(this, MyMapActivity.class);
        spec = tabHost.newTabSpec("map").setIndicator("",
                          res.getDrawable(R.drawable.ic_tab_map))
                      .setContent(intent);
        tabHost.addTab(spec);
        
     // list
        intent = new Intent().setClass(this, MyListActivity.class);
        spec = tabHost.newTabSpec("list").setIndicator("",
                          res.getDrawable(R.drawable.ic_tab_list))
                      .setContent(intent);
        tabHost.addTab(spec);
        
        // add
        intent = new Intent().setClass(this, AddActivity.class);
        spec = tabHost.newTabSpec("add").setIndicator("",
                          res.getDrawable(R.drawable.ic_tab_add))
                      .setContent(intent);
        tabHost.addTab(spec);

        
        // settings
        intent = new Intent().setClass(this, SettingsActivity.class);
        spec = tabHost.newTabSpec("sets").setIndicator("",
                          res.getDrawable(R.drawable.ic_tab_sets))
                      .setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);
    }
    
    @Override
    protected void onResume(){
    	super.onResume();
    }
    
    
    @Override
    protected void onDestroy(){
    	try {
    		((GeoPhoneRingsApp)getApplication()).getMyLocationsManager().serialize(this);
    		((GeoPhoneRingsApp)getApplication()).getSettings().serialize(this);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		if(((GeoPhoneRingsApp)getApplication()).getSettings().isStopAutomatically()){
			Intent svc = new Intent(this, LocationChangeService.class);
			stopService(svc);
		}else if(((GeoPhoneRingsApp)getApplication()).checkServiceRunning()){
			if(stateListener != null){
				stateListener.onApllicationOn(false);
			}
		}
		super.onDestroy();
    }
    
    // binding with service
    
	private void doBindService() {
		Intent myIntent = new Intent(this, my.georings.service.LocationChangeService.class);
		this.bindService(myIntent, boundConnection, Context.BIND_AUTO_CREATE);
	}

	private void doUnbindService() {
		this.unbindService(boundConnection);
	}
	
	private ServiceConnection boundConnection = new ServiceConnection() {
	    public void onServiceConnected(ComponentName className, IBinder service) {
	    	LocationChangeService boundService = ((LocationChangeService.LocationChangeBinder)service).getService();
	    	
	    	// get settings and MyLocationsManager
	    	boundService.setMyLocationsManager(((GeoPhoneRingsApp)getApplication()).getMyLocationsManager());
	    	stateListener = boundService;
	    	stateListener.onApllicationOn(true);
	    	
//	    	Log.d("MAIN............", "listener is connected");
	    	
	    	doUnbindService();
	    }
	
	    public void onServiceDisconnected(ComponentName className) {
	    	stateListener = null;
	    }
	};

	public void setStateListener(ApplicationStateListener stateListener) {
		this.stateListener = stateListener;
	}
    
	
    
}